<template>
  <MyDialog
    v-model="isVisible"
    :type="type"
    class="audit-dialog-wrapper"
    size="middle"
    :dialog-title="dialogTitle"
    :need-modal="needModal"
    @closed="closed"
  >
    <my-form
      ref="ruleForm"
      v-model="dataForm"
      label-width="120px"
      :disabled="currentRow.type"
      :model="dataForm"
      :is-grid="false"
      :rules="rules"
      :items-datas="state.itemsDatas"
    >
    </my-form>
    <template #footer>
      <my-button type="reset" title="关闭" @click="closed" />
      <my-button type="save" title="提交" :loading="buttonLoading" @click="submitForm" />
    </template>
  </MyDialog>
</template>

<script setup lang="ts" name="audit">
import { ref } from 'vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const props = defineProps(
  {
    dialogTitle: {
      type: String,
      default: '弹窗标题'
    },
    needModal: {
      type: Boolean,
      default: true
    },
    modelValue: {
      type: Boolean,
      default: false
    },
    currentRow: {
      type: Object,
      default: () => {}
    }
  },
  ['modelValue']
);
const buttonLoading = ref(false);
const ruleForm = ref<ElFormInstance>();
const state = reactive({
  dataForm: {
    examinedataId: '',
    examineStatus: '',
    examineRemark: ''
  },
  itemsDatas: [
    {
      label: '审核结论',
      prop: 'examineStatus',
      type: 'select',
      span: 24,
      options: [
        { value: '1', label: '审核通过' },
        { value: '2', label: '审核不通过' }
      ]
    },
    { label: '审核意见', prop: 'examineRemark', type: 'textarea', span: 24 }
  ],
  rules: {
    examineStatus: [{ required: true, message: '请选择', trigger: 'blur' }]
  }
});
const { dataForm, rules } = toRefs(state);
const isVisible = ref(false);
watch(
  () => props.modelValue,
  async (v) => {
    isVisible.value = v;
    if (props.currentRow.id) {
      dataForm.value.examinedataId = props.currentRow.id;
    }
  },
  { immediate: true, deep: true }
);
const emit = defineEmits(['update:modelValue']);
const closed = () => {
  buttonLoading.value = false;
  emit('update:modelValue');
};
/** 表单重置 */
const reset = () => {
  ruleForm.value?.resetFields();
};

/** 提交按钮 */
const submitForm = () => {
  ruleForm.value?.validate(async (valid) => {
    if (valid) {
      buttonLoading.value = true;
      proxy.$emit('auditSumbit', dataForm.value);
      reset();
      closed();
    }
  });
};
</script>
